//
//  HZQDatePickerView.m
//  HZQDatePickerView
//
//  Created by 1 on 15/10/26.
//  Copyright © 2015年 HZQ. All rights reserved.
//

#import "HZQDatePickerView.h"

//屏幕的宽度
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
//屏幕的高度
#define ScreenHeight [UIScreen mainScreen].bounds.size.height

@interface HZQDatePickerView ()

@property (nonatomic, strong) NSString *selectDate;

@property (strong, nonatomic) UIButton *cannelBtn;
@property (strong, nonatomic) UIButton *sureBtn;

@property (nonatomic,strong)UIControl *overlayView;

@end

@implementation HZQDatePickerView


-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self initView];
    }
    return self;
}

- (void)initView
{
    self.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.datePickerView = [[UIDatePicker alloc]init];
    [self addSubview:self.datePickerView];
    self.datePickerView.datePickerMode = UIDatePickerModeDate;
    self.datePickerView.backgroundColor = [UIColor whiteColor];
    [self.datePickerView makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.bottom).with.offset(0);
        make.left.equalTo(self.left).with.offset(0);
        make.right.equalTo(self.right).with.offset(0);
    }];
    
    self.cannelBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self addSubview:self.cannelBtn];
    [self.cannelBtn makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.datePickerView.top).with.offset(0);
        make.left.equalTo(self.left).with.offset(0);
        make.top.equalTo(self.top).with.offset(0);
        make.width.mas_equalTo(130);
    }];
    [self.cannelBtn setTitle:@"取消" forState:UIControlStateNormal];
    
    [self.cannelBtn addTarget:self action:@selector(removeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.sureBtn =[UIButton buttonWithType:UIButtonTypeSystem];
    [self addSubview:self.sureBtn];
    [self.sureBtn makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.datePickerView.top).with.offset(0);
        make.right.equalTo(self.right).with.offset(0);
        make.top.equalTo(self.top).with.offset(0);
        make.width.mas_equalTo(130);
    }];
    [self.sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [self.sureBtn addTarget:self action:@selector(sureBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    _overlayView = [[UIControl alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    _overlayView.backgroundColor = [UIColor colorWithRed:.16 green:.17 blue:.21 alpha:.5];
    [_overlayView addTarget:self
                     action:@selector(dismiss)
           forControlEvents:UIControlEventTouchUpInside];
    
}

- (NSString *)timeFormat
{
    NSDate *selected = [self.datePickerView date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *currentOlderOneDateStr = [dateFormatter stringFromDate:selected];
    return currentOlderOneDateStr;
}

// 取消
- (void)removeBtnClick:(UIButton *)sender {
    // 开始动画
    [self dismiss];
    NSLog(@"sdfsdf");
}

// 确定
- (void)sureBtnClick:(UIButton *)sender {
    // 开始动画
    self.selectDate = [self timeFormat];
    //delegate
    [self.delegate getSelectDate:self.selectDate type:self.type];
    [self dismiss];
}


#pragma mark - animations

- (void)fadeIn
{
    self.transform = CGAffineTransformMakeScale(1.2, 1.2);
    self.alpha = 0;
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1;
        self.transform = CGAffineTransformMakeScale(1, 1);
    }];
    
}
- (void)fadeOut
{
    [UIView animateWithDuration:0.3 animations:^{
        self.transform = CGAffineTransformMakeScale(1.2, 1.2);
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        if (finished) {
            [_overlayView removeFromSuperview];
            [self removeFromSuperview];
        }
    }];
}


- (void)show
{
    UIWindow *keywindow = [[UIApplication sharedApplication] keyWindow];
    [keywindow addSubview:_overlayView];
    [keywindow addSubview:self];
    
    [self makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(keywindow.bottom).with.offset(0);
        make.left.equalTo(keywindow).with.offset(0);
        make.right.equalTo(keywindow).with.offset(0);
        make.height.mas_equalTo(self.bounds.size.height);
    }];
    [self fadeIn];
}

- (void)dismiss
{
    [self fadeOut];
}

@end


